<?php
set_time_limit(0);

require_once('../classes/core.php');

class ws {
    
    private  $client;
    private  $soapUrl;
    
    function ws($url) {
        
        $this->soapUrl = $url;
        
        try{
            $this->client = new SoapClient($this->soapUrl,array("login"=>"wsuser", "password"=>"",'cache_wsdl' => WSDL_CACHE_NONE,'trace' => 1));
        }
        catch (Exception $e){
            echo $e->getMessage();
        }
    }

    function GetRealizations ($DocumentID = NULL, $StartDate = NULL, $EndDate = NULL){
        return $this->client->GetRealizations(array('DocumentID' =>$DocumentID, 'StartDate' => $StartDate, 'EndDate' => $EndDate));
    }
    
};

$ws     = new ws('http://212.72.152.66/smiley/ws/GetRealizations.1cws?wsdl');

$c_date = date("Y-m-d");

$client = $ws->GetRealizations('', $c_date, $c_date);

$count1 = count($client->return->RealizationsTable);

for ($i = 0; $i < $count1; $i++) {
    
    $user 							= $_SESSION['USERID'];

    $DocumentID						= $client->return->RealizationsTable[$i]->DocumentID;
    $Date							= $client->return->RealizationsTable[$i]->Date;
    $CustomerName 					= $client->return->RealizationsTable[$i]->CustomerName;
    $Customer1CCode 				= $client->return->RealizationsTable[$i]->Customer1CCode;
    $Agreement 						= $client->return->RealizationsTable[$i]->Agreement;
    $CustomerID 					= $client->return->RealizationsTable[$i]->CustomerID;
    $CustomerPhone 					= $client->return->RealizationsTable[$i]->CustomerPhone;
    $CustomerAddress 				= $client->return->RealizationsTable[$i]->CustomerAddress;
    $StoreHouse 					= $client->return->RealizationsTable[$i]->StoreHouse;
    $Subdivision 					= $client->return->RealizationsTable[$i]->Subdivision;
    $Responsible 					= $client->return->RealizationsTable[$i]->Responsible;
    
    $WaybillNum 					= $client->return->RealizationsTable[$i]->WaybillNum;
   	$WaybillID 						= $client->return->RealizationsTable[$i]->WaybillID;
    $WaybillStatus 					= $client->return->RealizationsTable[$i]->WaybillStatus;
    $WaybillTransportation 			= $client->return->RealizationsTable[$i]->WaybillTransportation;
    $WaybillTransportationType 		= $client->return->RealizationsTable[$i]->WaybillTransportationType;
    $WaybillMeanOfTransport 		= $client->return->RealizationsTable[$i]->WaybillMeanOfTransport;
    $WaybillTransporter 			= $client->return->RealizationsTable[$i]->WaybillTransporter;
    $WaybillTransporterID 			= $client->return->RealizationsTable[$i]->WaybillTransporterID;
    $WaybillActivationDate 			= $client->return->RealizationsTable[$i]->WaybillActivationDate;
    $WaybillTransportationPayer 	= $client->return->RealizationsTable[$i]->WaybillTransportationPayer;
    $WaybillTransportationExpence 	= $client->return->RealizationsTable[$i]->WaybillTransportationExpence;
    $WaybillDonor 					= $client->return->RealizationsTable[$i]->WaybillDonor;
    $WaybillRecivier 				= $client->return->RealizationsTable[$i]->WaybillRecivier;
    $WaybillNote 					= $client->return->RealizationsTable[$i]->WaybillNote;
    $WaybillRecieveDate 			= $client->return->RealizationsTable[$i]->WaybillRecieveDate;
    
    $count                          = count($client->return->RealizationsTable[$i]->Nomenclature);
    
    $result = mysql_query("SELECT realizations.id
				    		FROM  realizations
				    		WHERE realizations.DocumentID='$DocumentID' AND realizations.Date=Date(NOW())");
    
    $check = mysql_num_rows($result);
    
    if($check==0){
    
   mysql_query("INSERT INTO `realizations` 
					   (`user_id`, `DocumentID`, `Date`, `CustomerName`, `Customer1CCode`, `Agreement`, `CustomerID`, `CustomerPhone`, `phone2`, `phone3`, `mail`, `justin_adress`, `CustomerAddress`, `sity`, `fostal_code`, `fac_postal_cide`, `fact_adress`, `fact_sity`, `StoreHouse`, `Subdivision`, `Responsible`, `WaybillNum`, `WaybillID`, `WaybillStatus`, `WaybillTransportation`, `WaybillTransportationType`, `WaybillMeanOfTransport`, `WaybillTransporter`, `WaybillTransporterID`, `WaybillActivationDate`, `WaybillTransportationPayer`, `WaybillTransportationExpence`, `WaybillDonor`, `instalation_date`, `WaybillRecivier`, `WaybillNote`, `born_date`, `WaybillRecieveDate`, `comment`, `actived`) 
				 VALUES 
					   ( '$user', '$DocumentID', '$Date', '$CustomerName', '$Customer1CCode', '$Agreement', '$CustomerID', '$CustomerPhone', NULL, NULL, NULL, NULL, '$CustomerAddress', NULL, NULL, NULL, NULL, NULL, '$StoreHouse', '$Subdivision', '$Responsible', '$WaybillNum', '$WaybillID', '$WaybillStatus', '$WaybillTransportation', '$WaybillTransportationType', '$WaybillMeanOfTransport', '$WaybillTransporter', '$WaybillTransporterID', '$WaybillActivationDate', '$WaybillTransportationPayer', '$WaybillTransportationExpence', '$WaybillDonor', '', '$WaybillRecivier', '$WaybillNote', NULL, ' $WaybillRecieveDate', NULL, '1');");
    
    $realization_id = mysql_insert_id();
    }
    if ($count == 1) {
        
            $user					= $_SESSION['USERID'];
        
            $NomenclatureName 		= $client->return->RealizationsTable[$i]->Nomenclature->NomenclatureName;
            $NomenclatureSeries 	= $client->return->RealizationsTable[$i]->Nomenclature->NomenclatureSeries;
            $NomenclatureProperty 	= $client->return->RealizationsTable[$i]->Nomenclature->NomenclatureProperty;
            $NomenclatureCount 		= $client->return->RealizationsTable[$i]->Nomenclature->NomenclatureCount;
            $VATRate 				= $client->return->RealizationsTable[$i]->Nomenclature->VATRate;
            $Price 					= $client->return->RealizationsTable[$i]->Nomenclature->Price;
            $Discount 				= $client->return->RealizationsTable[$i]->Nomenclature->Discount;
            $Sum 					= $client->return->RealizationsTable[$i]->Nomenclature->Sum;
            $VAT 					= $client->return->RealizationsTable[$i]->Nomenclature->VAT;
            
          
            
            $result = mysql_query("SELECT nomenclature.realizations_id
									FROM nomenclature
									WHERE nomenclature.realizations_id='$realization_id'");
            
            $check2 = mysql_num_rows($result);
            
            if($check2==0){
            mysql_query("INSERT INTO `nomenclature` 
								(`user_id`, `realizations_id`, `NomenclatureName`, `NomenclatureSeries`, `NomenclatureProperty`, `NomenclatureCount`, `VATRate`, `Price`, `Discount`, `Sum`, `VAT`, `actived`) 
						 VALUES 
								('$user', '$realization_id', '$NomenclatureName', '$NomenclatureSeries', '$NomenclatureProperty', '$NomenclatureCount', '$VATRate', '$Price', '$Discount', '$Sum', '$VAT', '1');");
            } 
            
    }else {
    
        for ($j = 0; $j < $count; $j++) {
        	
            
        	$user					= $_SESSION['USERID'];
        	
        	$NomenclatureName 		= $client->return->RealizationsTable[$i]->Nomenclature[$j]->NomenclatureName;
            $NomenclatureSeries 	= $client->return->RealizationsTable[$i]->Nomenclature[$j]->NomenclatureSeries;
            $NomenclatureProperty 	= $client->return->RealizationsTable[$i]->Nomenclature[$j]->NomenclatureProperty;
            $NomenclatureCount 		= $client->return->RealizationsTable[$i]->Nomenclature[$j]->NomenclatureCount;
            $VATRate 				= $client->return->RealizationsTable[$i]->Nomenclature[$j]->VATRate;
            $Price 					= $client->return->RealizationsTable[$i]->Nomenclature[$j]->Price;
            $Discount 				= $client->return->RealizationsTable[$i]->Nomenclature[$j]->Discount;
            $Sum 					= $client->return->RealizationsTable[$i]->Nomenclature[$j]->Sum;
            $VAT 					= $client->return->RealizationsTable[$i]->Nomenclature[$j]->VAT;

           
            $result = mysql_query("SELECT nomenclature.realizations_id
									FROM nomenclature
									WHERE nomenclature.realizations_id='$realization_id'");
            
            $check1 = mysql_num_rows($result);
            
            if($check1==0){    
          
       mysql_query("INSERT INTO `nomenclature`
    					        (`user_id`, `realizations_id`, `NomenclatureName`, `NomenclatureSeries`, `NomenclatureProperty`, `NomenclatureCount`, `VATRate`, `Price`, `Discount`, `Sum`, `VAT`, `actived`)
    				     VALUES
    					        ('$user', '$realization_id', '$NomenclatureName', '$NomenclatureSeries', '$NomenclatureProperty', '$NomenclatureCount', '$VATRate', '$Price', '$Discount', '$Sum', '$VAT', '1');");
            
            }  
        }
        
    }
    
   
   
}
$data = array("key" => "547");
echo json_encode($data);

$time = time()."\n";

file_put_contents("log.txt", $time, FILE_APPEND);

?>
